﻿package com.vinyl.components
{
	import fl.core.UIComponent;
	import flash.events.*;
	import flash.events.Event;
	import flash.display.MovieClip;
	
	import com.vinyl.controls.Button;
	import com.vinyl.controls.ButtonView;
	import com.vinyl.controls.SimpleRadioButtonView;
	
	import com.vinyl.controls.RadioGroupManager;
	import com.vinyl.controls.RadioGroup;
	import com.vinyl.controls.IRadioGroupInfo;
	
	
	[IconFile ("../../../../../comoponents/02_simpleRadioButton/assets/radioButton.png")]
	public class SimpleRadioButton extends UIComponent 
	{ 
		public static var EVENT_CHANGED: String = 'button_changed';
		public var boundingBox:MovieClip;
		public var value;
		
		private var sbv:SimpleRadioButtonView;
		private var btn:Button;
		
		private var fontCol:Number; 
		
		[Inspectable( type=String, defaultValue="group1" ) ]
		public function set groupName(name:String):void { 
			btn.groupName = name;
			crateRadioGroup();
		}
		
		public function get groupName():String { 
			return btn.groupName; 
		}
		
		public function setSkin(bBiew: ButtonView ):void
		{
			removeChild( sbv );
			btn.removeEventListener( Button.EVENT_CHANGED, onButtonActionHandler );
			
			btn = new Button( bBiew, false );
			btn.addEventListener( Button.EVENT_CHANGED, onButtonActionHandler );
			addChild( bBiew );
		}
		
		public function setValue( value ):void
		{
			btn.setValue( value );
		}
		
		public function SimpleRadioButton() 
		{ 
			super();
			sbv = new SimpleRadioButtonView();			
			btn = new Button( sbv, false );
			btn.addEventListener( Button.EVENT_CHANGED, onButtonActionHandler );
			
			addChild( sbv );
			
			init();
		}
		
		override protected function configUI():void
		{
			super.configUI();
		}
		
		private function crateRadioGroup()
		{
			var rgi = RadioGroupManager.getInstance().getRadioGroupInfo( btn.groupName );
			rgi.addRadioButton( btn )
		}
	
		private function init() 
		{ 
			boundingBox.width = 0;
			boundingBox.height = 0;
			boundingBox.visible = false;
		}
	
		private function onButtonActionHandler( evt ): void
		{
			value = evt.target.getValue();
			dispatchEvent( new Event( EVENT_CHANGED ) );
		}
	}
}
